package com.shopx.core.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @ClassName: StringUtil
 * @Description: 工具类：字符串处理
 * @version: 1.0
 * @author: lituo
 * @Create: 2014-3-17
 */
public class StringUtil {

	/**
	 * 字符截取
	 * @param str    源字符串
	 * @param first  截取初始点
	 * @param second 截取结束点
	 * @return
	 */
	public static String getStringBetweenKeys(String str, String first, String second) {
		if (str != null && first != null && second != null) {
			// 判断第一个查找串位置
			int firstIndex = str.indexOf(first);
			if (firstIndex != -1) {
				firstIndex += first.length();
				int secondIndex = str.indexOf(second, firstIndex);
				if (secondIndex == -1) {
					secondIndex = str.length();
				}
				str = str.substring(firstIndex, secondIndex);
			} else {
				str = "";
			}
		}
		return str;
	}

	public static String emptyValue(String s){
		if(StringUtil.isEmpty(s)){
			return " ";
		}else{
			return s;
		}
	}

	/**
	 *
	 * @Title: strSplit
	 * @Description: 把一个字符串按照标示组合成一个数组
	 * @param @param
	 *            str
	 * @param @param
	 *            split
	 * @param @return
	 * @return String[]
	 * @throws @author:
	 *             lituo
	 * @Create: 2013-7-6
	 */
	public static String[] strSplit(String str, String split) {
		if (str != null) {
			if (split != null) {
				return str.split(split);
			} else {
				return new String[] { str };
			}
		} else {
			return new String[0];
		}
	}

	/**
	 *
	 * @Title: isEmpty
	 * @Description: 判断字符串是否为空
	 * @param @param
	 *            str
	 * @param @return
	 * @return boolean
	 * @throws @author:
	 *             lituo
	 * @Create: 2013-7-6
	 */
	public static boolean isEmpty(String str) {
		if (null == str) {
			return true;
		}
		if ("".equals(str.trim())) {
			return true;
		}
		return false;
	}

	/**
	 *
	 * @Title: ExceptionToString
	 * @Description: 异常转换为字符串
	 * @param @param
	 *            e
	 * @param @return
	 * @return String
	 * @throws @author:
	 *             lituo
	 * @Create: 2013-7-18
	 */
	public static String ExceptionToString(Exception e) {
		StringBuffer sb = new StringBuffer();
		sb.append(e.toString());
		StackTraceElement[] stes = e.getStackTrace();
		for (StackTraceElement ste : stes) {
			sb.append("\n" + ste.toString());
		}
		return sb.toString();
	}

	/**
	 *
	 * @Title: arrayUnique
	 * @Description: 数组去重
	 * @param: @param
	 *             a
	 * @param: @return
	 * @return: String[]
	 * @throws @author
	 *             lituo
	 * @Date 2015-4-8 下午03:11:06
	 */
	public static String[] arrayUnique(String[] array) {
		// array_unique
		List<String> list = new LinkedList<String>();
		for (String s : array) {
			if (!list.contains(s)) {
				list.add(s);
			}
		}
		return (String[]) list.toArray(new String[list.size()]);
	}

	public static String getCode(int passLength, int type) {
		StringBuffer buffer = null;
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		r.setSeed(new Date().getTime());
		switch (type) {
		case 0:
			buffer = new StringBuffer("0123456789");
			break;
		case 1:
			buffer = new StringBuffer("abcdefghijklmnopqrstuvwxyz");
			break;
		case 2:
			buffer = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
			break;
		case 3:
			buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
			break;
		case 4:
			buffer = new StringBuffer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
			sb.append(buffer.charAt(r.nextInt(buffer.length() - 10)));
			passLength -= 1;
			break;
		case 5:
			String s = UUID.randomUUID().toString();
			sb.append(s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23)
					+ s.substring(24));
		}

		if (type != 5) {
			int range = buffer.length();
			for (int i = 0; i < passLength; ++i) {
				sb.append(buffer.charAt(r.nextInt(range)));
			}
		}
		return sb.toString();
	}

	/**
	 * 下划线转驼峰
	 *
	 * @param line
	 *            源字符串
	 * @param smallCamel
	 *            大小驼峰,是否为小驼峰
	 * @return 转换后的字符串
	 */
	public static String underline2Camel(String line, boolean smallCamel) {
		if (line == null || "".equals(line)) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
		Matcher matcher = pattern.matcher(line);
		while (matcher.find()) {
			String word = matcher.group();
			sb.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0))
					: Character.toUpperCase(word.charAt(0)));
			int index = word.lastIndexOf('_');
			if (index > 0) {
				sb.append(word.substring(1, index).toLowerCase());
			} else {
				sb.append(word.substring(1).toLowerCase());
			}
		}
		return sb.toString();
	}

	/**
	 * 驼峰转下划线
	 *
	 * @param str
	 *            源字符串
	 * @param excludeStr
	 *            排除转义的字符串
	 * @return 转换后的字符串
	 */
	public static String camel2Underline(String str, String... excludeStr) {
		if (isEmpty(str)) {
			return "";
		}
		for (String s : excludeStr) {
			if (s.equals(str)) {
				return str;
			}
		}
		str = String.valueOf(str.charAt(0)).toUpperCase().concat(str.substring(1));
		StringBuffer sb = new StringBuffer();
		Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()) {
			String word = matcher.group();
			sb.append(word.toUpperCase());
			sb.append(matcher.end() == str.length() ? "" : "_");
		}
		return sb.toString();
	}

	public static String createRandomCharData(int length) {
		StringBuilder sb = new StringBuilder();
		Random rand = new Random();// 随机用以下三个随机生成器
		Random randdata = new Random();
		int data = 0;
		for (int i = 0; i < length; i++) {
			int index = rand.nextInt(3);
			// 目的是随机选择生成数字，大小写字母
			switch (index) {
			case 0:
				data = randdata.nextInt(10);// 仅仅会生成0~9
				sb.append(data);
				break;
			case 1:
				data = randdata.nextInt(26) + 65;// 保证只会产生65~90之间的整数
				sb.append((char) data);
				break;
			case 2:
				data = randdata.nextInt(26) + 97;// 保证只会产生97~122之间的整数
				sb.append((char) data);
				break;
			}
		}
		String result = sb.toString();
		return result;
	}

	/**
	 * @descript 获取字符串含某个固定字符的个数
	 *
	 * @param str
	 * @param s
	 * @author lituo
	 * @date 2021/3/25 15:01
	 * @return int
	 */
	public static int countChar(String str,String s) {
		// 将字符串转换为字符数组
		char[] chs = str.toCharArray();
		// 定义变量count存储字符串出现次数
		int count = 0;
		for(int i = 0;i < chs.length;i++) {
			if(chs[i] == s.toCharArray()[0]) {
				count++;
			}
		}
		return count;
	}

	/**
	  * 防止SQL注入
	  * @author lituo
	  * @date 4:54 PM 2022/2/9
	  * @param	sql
	  * @return boolean
	  */
	public static boolean checkSqlStr(String sql){
		if(StringUtil.isEmpty(sql)){
			return false;
		}
		String keys[] = new String[]{";","delete","drop","truncate","insert","from"};
		for(String key:keys){
			if(sql.toLowerCase().indexOf(key)>-1){
				return true;
			}
		}
		return false;
	}


	public static String getNotEmptyStr(String s){
		if(StringUtil.isEmpty(s)){
			return "";
		}
		return s;
	}

	public static void main(String[] args) throws IOException {
		String s = "csd_id\n" +
				"region_name\n" +
				"org_code\n" +
				"org_name\n" +
				"cost_special_code\n" +
				"material_class_name\n" +
				"brand_category\n" +
				"supply_name\n" +
				"material_name\n" +
				"payed_flag\n" +
				"bill_type\n" +
				"bill_date\n" +
				"stock_num";
		StringBuffer strbuf=new StringBuffer();
		String line;
		BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(s.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));
		while ( (line = br.readLine()) != null ) {
			if(!line.trim().equals("")){
				String s2 = "";
				for(int i=0;i<line.split("_").length;i++){
					if(i==0){
						s2+=line.split("_")[i];
					}else{
						s2+=line.split("_")[i].substring(0,1).toUpperCase()+line.split("_")[i].substring(1);
					}
				}
				System.out.println(s2);
			}
		}

//		String s = StringUtil.createRandomCharData(24);
//		System.out.println(s);
	}
}
